package look4ss.aplicativo;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
import com.sdsmdg.tastytoast.TastyToast;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class ActivityApagar extends AppCompatActivity {

    public Context context;
    ProgressDialog dialogoLoading;
    public static String idEnvio = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_aba_info);
        dialogoLoading = new ProgressDialog(this);
        perguntar();
    }

    private void perguntar() {
        Context c = this;
        AlertDialog.Builder b = new AlertDialog.Builder(c);
        b.setTitle(R.string.exclusao_dados_titulo);
        b.setMessage(R.string.exclusao_dados_descricao);
        b.setCancelable(true);
        b.setIcon(R.drawable.icone_apagar);
        b.setPositiveButton(
                R.string.sim_apagar,
                new DialogInterface.OnClickListener() {
                    public void onClick(final DialogInterface d, int id) {
                        apagar();
                        d.cancel();
                    }
                });
        b.setNegativeButton(
                R.string.negativo_voltar,
                new DialogInterface.OnClickListener() {
                    public void onClick(final DialogInterface d, int id) {
                        d.cancel();
                        finish();
                    }
                });
        AlertDialog alerta = b.create();
        alerta.show();
    }

    private void apagar() {
        dialogoLoading.setMessage(getString(R.string.apagando_dados_selec));
        dialogoLoading.show();
        new Thread(new Runnable() {
            public void run() {
                try {
                    Document d = Jsoup.connect(variaveisGlobais.urlApiApagar)
                            .data("idEnvio", idEnvio)
                            .post();
                    TastyToast.makeText(getApplicationContext(), "Concluído!", Toast.LENGTH_SHORT, TastyToast.SUCCESS);
                } catch (Exception e) {
                    //k
                }
                finalizar();
            }
        }).start();
    }
    private void finalizar() {
        Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(i);
        System.exit(0);
    }
}